{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import imageio\n",
    "import glob"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# set path here\n",
    "feature_vis_path = 'data/feature-vis/inceptionV1/'\n",
    "filepaths = sorted(glob.glob(feature_vis_path + '*.jpg'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "553"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(filepaths)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "sw = 147 # sprite width and height\n",
    "dw = 74 # dataset example width and height, top row is 74x74, bottom row is 74x73, but we make all 73x73\n",
    "image_type = '.jpg'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for filepath in filepaths:\n",
    "    print(filepath)\n",
    "    layer = filepath.split(feature_vis_path)[1][0:7]\n",
    "    ch = int(filepath.split(feature_vis_path)[1][8:13])\n",
    "    # print(layer)\n",
    "    # print(ch, int(ch))\n",
    "    img = imageio.imread(filepath)\n",
    "    num_of_channels_in_img = img.shape[0]/sw\n",
    "    \n",
    "    if float(num_of_channels_in_img).is_integer():\n",
    "        for channel in range(int(num_of_channels_in_img)):\n",
    "            \n",
    "            # print(channel)\n",
    "            # plt.imshow(img[channel*sw:channel*sw+sw])\n",
    "            # plt.show()\n",
    "            \n",
    "            for fv in range(7):\n",
    "                curr_fv = img[channel*sw:channel*sw+sw, fv*sw:fv*sw+sw] #save here\n",
    "                \n",
    "                # neuron\n",
    "                if fv == 0:\n",
    "                    path_to_save = feature_vis_path + 'single-images-jpg/neuron/' + layer + '-' + str(int(ch) + channel) + '-neuron' + image_type\n",
    "                    imageio.imwrite(path_to_save, curr_fv)\n",
    "\n",
    "                # channel\n",
    "                if fv == 1:\n",
    "                    path_to_save = feature_vis_path + 'single-images-jpg/channel/' + layer + '-' + str(int(ch) + channel) + '-channel' + image_type\n",
    "                    imageio.imwrite(path_to_save, curr_fv)\n",
    "                    \n",
    "                # negative channel\n",
    "                if fv == 2:\n",
    "                    path_to_save = feature_vis_path + 'single-images-jpg/negative/' + layer + '-' + str(int(ch) + channel) + '-negative' + image_type\n",
    "                    imageio.imwrite(path_to_save, curr_fv)\n",
    "                    \n",
    "                # diversity 0\n",
    "                if fv == 3:\n",
    "                    path_to_save = feature_vis_path + 'single-images-jpg/diversity-0/' + layer + '-' + str(int(ch) + channel) + '-diversity-0' + image_type\n",
    "                    imageio.imwrite(path_to_save, curr_fv)\n",
    "                    \n",
    "                # diversity 1\n",
    "                if fv == 4:\n",
    "                    path_to_save = feature_vis_path + 'single-images-jpg/diversity-1/' + layer + '-' + str(int(ch) + channel) + '-diversity-1' + image_type\n",
    "                    imageio.imwrite(path_to_save, curr_fv)\n",
    "                    \n",
    "                # diversity 2\n",
    "                if fv == 5:\n",
    "                    path_to_save = feature_vis_path + 'single-images-jpg/diversity-2/' + layer + '-' + str(int(ch) + channel) + '-diversity-2' + image_type\n",
    "                    imageio.imwrite(path_to_save, curr_fv)\n",
    "                    \n",
    "                # diversity 3\n",
    "                if fv == 6:\n",
    "                    path_to_save = feature_vis_path + 'single-images-jpg/diversity-3/' + layer + '-' + str(int(ch) + channel) + '-diversity-3' + image_type\n",
    "                    imageio.imwrite(path_to_save, curr_fv)\n",
    "                    \n",
    "                # plt.imshow(img[channel*sw:channel*sw+sw, fv*sw:fv*sw+sw])\n",
    "                # plt.show()\n",
    "            \n",
    "            for de in range(10):\n",
    "                \n",
    "                # positve dataset examples\n",
    "                curr_de_p = img[channel*sw:channel*sw+dw-1, sw*7+de*dw:sw*7+de*dw+dw-1]\n",
    "                # print(img[channel*sw:channel*sw+dw-1, sw*7+de*dw:sw*7+de*dw+dw-1].shape)\n",
    "                # plt.imshow(img[channel*sw:channel*sw+dw-1, sw*7+de*dw:sw*7+de*dw+dw-1])\n",
    "                # plt.show()\n",
    "                path_to_save = feature_vis_path + 'single-images-jpg/dataset-p/' + layer + '-' + str(int(ch) + channel) + '-dataset-p-' + str(de) + image_type\n",
    "                imageio.imwrite(path_to_save, curr_de_p)\n",
    "                \n",
    "                # negative dataset examples\n",
    "                curr_de_n = img[channel*sw+dw:channel*sw+dw*2-1, sw*7+de*dw:sw*7+de*dw+dw-1]\n",
    "                # print(img[channel*sw+dw:channel*sw+dw*2-1, sw*7+de*dw:sw*7+de*dw+dw-1].shape)\n",
    "                # plt.imshow(img[channel*sw+dw:channel*sw+dw*2-1, sw*7+de*dw:sw*7+de*dw+dw-1])\n",
    "                # plt.show()\n",
    "                path_to_save = feature_vis_path + 'single-images-jpg/dataset-n/' + layer + '-' + str(int(ch) + channel) + '-dataset-n-' + str(de) + image_type\n",
    "                imageio.imwrite(path_to_save, curr_de_n)\n",
    "            \n",
    "    else:\n",
    "        print('sprite not multiple of ws?')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
